% Opens the points detected by ARTag and saved using a C program and
% prepares the data for the Matlab calibration routines

clear;

directory = '/home/faraz/matlab/edmund-camera/overhead_select/processed/';
im_directory = ('/home/faraz/matlab/edmund-camera/overhead_select/');

fileList = dir(directory);

valid_counter = 0;

for i=3:length(fileList)
    fname = sprintf('%s/%s',directory,fileList(i).name);
    eval([fileList(i).name '=dlmread(''' fname ''')'';']);
    valid_counter = valid_counter + 1;
    
    % There is a strange error with ARTag detection. Sometimes the same
    % marker is detected twice. Check for this here:
    tempX = eval(fileList(i).name);
    
    [rows,cols] = size(tempX);
	
	for j=1:cols
		for k=j+1:cols
	
			if norm( tempX(1:2,j) - tempX(1:2,k) ) == 0 
				
				% We have a duplicated object point
	   		    fprintf(1,'WARNING: Duplicate point detection in %s.\n', fileList(i).name );
				
			end;
		end;
	end;
    
end;

% There are several other variables that must be set to tell calibration
% how many images to use etc..
n_ima = valid_counter / 2;
active_images = ones(1,n_ima);
ind_active = [ 1:n_ima ];

nx = 1280;
ny = 1024;


% Now we can try out calibration.
go_calib_optim_no_read;